/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 * https://leetcode-cn.com/problems/merge-two-sorted-lists/
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
 var mergeTwoLists = function(l1, l2) {
    let dummyHead = new ListNode();
    let last = dummyHead;
    let p1 = l1;
    let p2 = l2;
    while (p1 && p2) {
        if (p1.val > p2.val) {
            last.next = p2;
            p2 = p2.next;
        } else {
            last.next = p1;
            p1 = p1.next;
        }
        last = last.next;
    }
    let rest = p1 || p2;
    while (rest) {
        last.next = rest;
        rest = rest.next;
        last = last.next;
    }
    return dummyHead.next;
};
